package com.selft;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;

/**
 * Created by xi on 2016/12/7.
 */
public class IndexAndMapping {

    public static void delDoc(Client client,String indexName,String type,String docId){
        client.prepareDelete(indexName, type, docId).execute().actionGet();
    }

    public static void delIndex(Client client,String indexName){
        client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();
    }


    public static void createIndex(Client client,String indexName){
        client.admin().indices().prepareCreate(indexName).get();
//        client.admin().indices().prepareCreate("twitter").addm

        //  client.admin().indices().prepareCreate(indexName).setSettings(settings).get();
    }

    //put是添加
    public static void createIndexWithRest(RestClient restClient,String indexName){
        String method = "PUT";
        String endpoint = "/"+indexName;
        Response response = null;
        try {
            response = restClient.performRequest(method,endpoint);
            System.out.println("createIndexWithRest:"+EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void createIndexWithSetting(Client client,String indexName){
        client.admin().indices().prepareCreate("twitter")
                .setSettings(Settings.builder()
                                .put("index.number_of_shards", 3)
                                .put("index.number_of_replicas", 2)
                )
                .get();
    }

    //更新索引的mapping  Updates the user mapping type. This user has now a new field user_name
    //验证下1.7 下的删除mapping之后，默认会删除索引的数据吗。
    //导入房源的时候，会删除房源的索引吗？
    public static void updateIndexMapping(Client client,String indexName,String typeName){
        client.admin().indices().preparePutMapping(indexName)
                .setType(typeName)
                .setSource("{\n" +
                        "  \"properties\": {\n" +
                        "    \"user_name\": {\n" +
                        "      \"type\": \"string\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}")
                .get();
    }

    //创建Mapping和更新mapping功能
    public static void createMapping(Client client,String indexName,String type) throws  Exception{
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject(type)
                .startObject("properties")
                .startObject("id").field("type", "long").endObject()
                .startObject("name").field("type", "text").field("store", "yes").field("index", "not_analyzed").endObject()
                //.startObject("time").field("type", "date").endObject()
                .startObject("time").field("type","date").endObject()
                .startObject("email").field("type","keyword").endObject()
                .startObject("email3").field("type","keyword").endObject()
                // v10 会变成long 类型 ? 这是为什么。。。。。。。
                //.startObject("edate").field("type", "date").field("store","yes").field("format", "yyyy-MM-dd HH:mm:ss.SSS").endObject()
                .endObject()
                .endObject()
                .endObject();
             PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(type).source(builder);
             client.admin().indices().putMapping(mappingRequest).actionGet();
    }

    public static void createMapping2(Client client,String indexName,String type) throws  Exception{
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject(type)
                .startObject("properties")
                .startObject("id").field("type", "long").endObject()
                .startObject("name").field("type","text").field("store", "yes").field("index", "not_analyzed").endObject()
                .startObject("time").field("type","long").endObject()
                .startObject("email").field("type","long").endObject()
                .startObject("email2").field("type","keyword").endObject()//mapping修改功能
                        // v10 会变成long 类型 ? 这是为什么。。。。。。。
                        //.startObject("edate").field("type", "date").field("store","yes").field("format", "yyyy-MM-dd HH:mm:ss.SSS").endObject()
                .endObject()
                .endObject()
                .endObject();
        PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(type).source(builder);
        client.admin().indices().putMapping(mappingRequest).actionGet();
    }

    public static String initSingleDocument(){
        String json = "";
        try {
            XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();
            contentBuilder.field("id",1);
            contentBuilder.field("name", "xiaocai");
            contentBuilder.field("email", "93");
            contentBuilder.field("time",2);
            json = contentBuilder.endObject().string();
            System.out.println("json==" + json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
